package com.spring.log4j2.utils;

import org.apache.log4j.MDC;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by devsun on 2017/10/31.
 */
@WebFilter(urlPatterns = "/*")
public class LoggerMDCFilter extends OncePerRequestFilter implements Filter{

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        try {
            MDC.put("userID" , 234397898);
            filterChain.doFilter(request, response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
